package questionanswer;


import java.io.Serializable;
import java.util.Arrays;
import java.util.Collections;

public class MultipleAnswer implements Serializable, Answer {
    private String[] keyAnswers;
    private String[] answers;

    public MultipleAnswer(String[] keyAnswers, String[] answers) {
        this.keyAnswers = keyAnswers;
        this.answers = answers;
    }

    public MultipleAnswer(MultipleAnswer original) {
        int answersLength = original.answers.length;
        int keysLength = original.keyAnswers.length;
        this.answers = Arrays.copyOf(original.answers, answersLength);
        this.keyAnswers = Arrays.copyOf(original.keyAnswers, keysLength);

    }

    @Override
    public void shuffleAnswers() {
        Collections.shuffle(Arrays.asList(answers));
    }

    private int[] getAnswerAsArray(String userAnswer) {
        int length = userAnswer.length();
        int userAnswerAsInt = Integer.parseInt(userAnswer);
        int[] userAnswerAsIntArray = new int[length];
        int digitTaker = 10;
        for (int i = 0; i < length; i++) {
            userAnswerAsIntArray[i] =  (userAnswerAsInt % digitTaker) - 1;
            userAnswerAsInt /= digitTaker;
        }
        return userAnswerAsIntArray;
    }

    private boolean isEqualLength(String userAnswer) {
        return userAnswer.length() == keyAnswers.length;
    }

    public boolean isCorrect(String userAnswer) {
        if (!isEqualLength(userAnswer)) {
            return false;
        }
        int[] userAnswerAsIntArray = getAnswerAsArray(userAnswer);
        if (!isInsideBounds(userAnswerAsIntArray)) {
            return false;
        }

        cutKeyAnswers(userAnswerAsIntArray);

        return checkKeyAnswersExistence();

    }

    private boolean isInsideBounds(int[] userAnswerAsIntArray) {
        for (int elem: userAnswerAsIntArray) {
            if ((elem < 0) || (elem >= answers.length)){
                return false;
            }
        }
        return true;
    }

    private boolean checkKeyAnswersExistence() {
        for (String keyAnswer : keyAnswers) {
            if (!keyAnswer.equals("")) {
                return false;
            }
        }
        return true;
    }

    private void cutKeyAnswers(int[] userAnswerAsIntArray) {
        for (int chosenElement = 0; chosenElement < keyAnswers.length; chosenElement++) {
            int userAnswer = userAnswerAsIntArray[chosenElement];
            for (int keyElement = 0; keyElement < keyAnswers.length; keyElement++){
                if (answers[userAnswer].equals(keyAnswers[keyElement])) {
                    keyAnswers[keyElement] = "";
                }
            }
        }
    }

    @Override
    public String toString() {
        String result = "";
        for (int answerNumber = 0; answerNumber < answers.length; answerNumber++) {
            result += String.format("%d) %s%n", answerNumber + 1, answers[answerNumber]);
        }
        return result;
    }
}
